﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;

namespace IHISCETeim.Web.MachineSys.UserModel
{
    public partial class UWarrantyCardAdd : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.新增 };
            }
        }

        protected override string DWZRef
        {
            get { return "UWarrantyCardList"; }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "add")
            {
                addmenu();
                
            }
        }

        protected void addmenu()
        {
            OJobInfo obj = Request.SerializableObject<OJobInfo>();
            // obj.UserType = 1;
            UWarrantyCardInfo objUWC = Request.SerializableObject<UWarrantyCardInfo>();
            objUWC.AgentName = GetRequestValue<string>("dwz.orgLookup.AgentName","");
            objUWC.AgentAddress = GetRequestValue<string>("dwz.orgLookup.AgentAddress", "");

            ;//string.Format("{0}省{1}市{2}县(街){3}乡(镇){4}村", objUWC.CustomerAddress.Split(new char[]{ ','}));
            string[] caddresss = objUWC.CustomerAddress.Split(new char[] { ',' });
            string[] caddresssuff=new string[]{"省","市",",县(街)","乡(镇)","村"};
            int i = 0;
            objUWC.CustomerAddress = "";
            foreach (string item in caddresss)
            {
                if(!string.IsNullOrEmpty(item))
                    objUWC.CustomerAddress +=item+caddresssuff[i];
                i++;
            }


            UWarrantyCardInfo objtest=   UWarrantyCardController.GetBySerialNumber(objUWC.SerialNumber.Trim().ToUpper());
            if (objtest.UWCID > 0)
            {
                AlertJson("对不起,机号" + objUWC.SerialNumber + "已存在.请重新输入");
                return;
            }

            objUWC.UWCID=UWarrantyCardController.Add(objUWC);

            List<UQuestionTempleteInfo> allQtemp = UWarrantyCardController.GetAllUQuestionTempleteByParId(0);
         
            List<UQuestionnaireInfo> alluq = new List<UQuestionnaireInfo>();
            foreach (UQuestionTempleteInfo item in allQtemp)
            {
                string[] xx = GetRequestValue<string>("uqt" + item.UQTID.ToString(),"").Split(new char[]{','});
                foreach (string sitem in xx)
                {
                    if (sitem.IsEmptyOrNull())
                        continue;
                    UQuestionnaireInfo uqtitem= new UQuestionnaireInfo();
                    uqtitem.UQTID = Convert.ToInt32(sitem); ;
                    uqtitem.UWCID = objUWC.UWCID;
                    uqtitem.SerialNumber = objUWC.SerialNumber;
                    uqtitem.InputValue = "";
                    alluq.Add(uqtitem);
                }
            }
            UWarrantyCardController.AddUQuestionnaire(alluq);
            AddLog(Permission.新增, objUWC);
            SuccessJson("添加成功", this.DWZRef);
        }

        #region 获取调查表模板　
        protected string GetUQuestionTemplete()
        {
            string TempHtml = "";
            TempHtml = UWarrantyCardController.DC.Get<string>(DataCacheKey.UQuestionTempleteDCKey);
            if (!TempHtml.IsEmptyOrNull())
                return TempHtml;  
            TempHtml="";
            string ItemTmpHtmlTitleTemp = "<b>({0}){1}</b>";
            string ItemTmpHtmlConTemp = "<table>{0}</table><div class=\"divider\"></div> ";
            List<UQuestionTempleteInfo> allQtemp= UWarrantyCardController.GetAllUQuestionTempleteByParId(0);
           
            foreach (UQuestionTempleteInfo item in allQtemp)
            {
                TempHtml = TempHtml + string.Format(ItemTmpHtmlTitleTemp,item.OrderId, item.Title);
                List<UQuestionTempleteInfo> AllitemTemp = UWarrantyCardController.GetAllUQuestionTempleteByParId(item.UQTID);
                string trhtml = "";
                int tdnum =0;
               // item.RowTDNum = item.RowTDNum+1;
                trhtml = "<tr>";
                foreach (UQuestionTempleteInfo item2 in AllitemTemp)
                {
                    if ((tdnum % item.RowTDNum) == 0 && tdnum!=0)
                    {
                        trhtml = trhtml + "</tr><tr>";
                    } 
                    trhtml = trhtml +string.Format( "<td><label><input type=\"checkbox\" name=\"uqt{0}\" value=\"{1}\">{2}</label></td>",item.UQTID,item2.UQTID,item2.Title);
                    tdnum++;
                }
                trhtml = trhtml + "</tr>";

                TempHtml = TempHtml + string.Format(ItemTmpHtmlConTemp, trhtml);
                
            }

            UWarrantyCardController.DC.Add(DataCacheKey.UQuestionTempleteDCKey, TempHtml);
            return TempHtml;
        }
        #endregion
    }
}